package com.itcast.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.itcast.pojo.Goods;
import com.itcast.pojo.Orders;
import com.itcast.service.Ordersservice;
import com.itcast.utils.Respone;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@RestController
@RequestMapping("/orders")
@Slf4j
public class OrdersController {
    @Autowired
    private Ordersservice ordersservice;



    @RequestMapping("/index")
    @ResponseBody
    public Map<String, Object> index() throws  ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd");
        Map<String, Object> map = new HashMap<>();
        List<String> list1=new ArrayList<>();
        List<Integer> list2=new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            List<String> countorders = ordersservice.countorders(i);

            if (countorders.size()!=0){
                String source = countorders.get(0);
                Date parse = sdf.parse(source);
                String format = sdf.format(parse);
                list1.add(format);
                map.put("date", list1);
                list2.add(countorders.size());
                map.put("size", list2);
            }else {
                list1.add("");
                map.put("date", list1);
                list2.add(0);
                map.put("size", list2);
            }

        }
        return map;
    }

    @GetMapping("/list")
    public Respone list(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("ooId") Integer ooId) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ooid", ooId);
        Page<Orders> pageSize = new Page<>(page, limit);
        IPage<Orders> page1 = ordersservice.page(pageSize, queryWrapper);
        System.out.println((int) page1.getTotal());
        return new Respone(0, "成功", page1.getRecords(), (int) page1.getTotal());
    }

    @PostMapping("/update")
    public Respone update(@RequestBody Orders orders) {
        orders.setUpDates(new Date());
        orders.setSubtotal(orders.getCount() * orders.getGoodsPrice());
        boolean update = ordersservice.updateById(orders);
        if (update) {
            return new Respone(0, "更新成功");
        }
        return new Respone(999, "更新失败");
    }

    @GetMapping("/delete")
    public Respone del(@RequestParam("oid") Integer oid) {
        if (ordersservice.removeById(oid)) {
            return new Respone(0, "删除成功");
        }
        return new Respone(999, "删除失败");
    }


}

